package com.geek.concurrence4work;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @Author: lishuangqiang
 * @Date: 2021/11/28
 * @Description:
 */

public class LockSupportMethod {

    private static volatile long res = 0;


    private void doSum() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        res = Homework03.sum3();
        LockSupport.park(this);
    }

    private long getRes(Thread thread) {
        while (true) {
            if (0 != res) {
                LockSupport.unpark(thread);
                return res;
            }
        }

    }


    public static void main(String[] args) throws InterruptedException {
        LockSupportMethod lockSupportMethod = new LockSupportMethod();
        Thread thread = new Thread(() -> {
            lockSupportMethod.doSum();
        });
        thread.start();
        System.out.println(lockSupportMethod.getRes(thread));
    }
}
